﻿#include "MaterialSubsystem.h"
#include "Shader.h"
#include "Material.h"
#include "Path/Paths.h"

#include "Asset/EngineDefaultPackage.h"

static RMaterialSubsystem* Instance=nullptr;

void RMaterialSubsystem::Initialize()
{
    DefaultVertexShader=REngineDefaultPackage::Get()->GetDefaultMeshVertexShader();
    DefaultFragmentShader=REngineDefaultPackage::Get()->GetDefaultMeshFragmentShader();

    Instance=this;
}

void RMaterialSubsystem::Deinitialize()
{
    Instance=nullptr;
}

RMaterialSubsystem *RMaterialSubsystem::Get()
{
    return Instance;
}

TSharedObjectPtr<RMaterialSubsystem> RMaterialSubsystem::GetInstance()
{
    return TSharedObjectPtr<RMaterialSubsystem>(Instance);
}

TSharedObjectPtr<RMaterial> RMaterialSubsystem::CreateDefaultMaterial()
{
    return CopyMaterial(REngineDefaultPackage::Get()->GetDefaultMaterial());
}

TSharedObjectPtr<RMaterial> RMaterialSubsystem::CreateSingleColorMaterial(const SColor &InColor)
{
    RMaterial::SMaterialCreateInfo CreateInfo;
    CreateInfo.VertexShader=DefaultVertexShader;
    CreateInfo.FragmentShader=REngineDefaultPackage::Get()->GetSingleColorFragmentShader();
    CreateInfo.PipelineType=ERenderMaterialType::Opaque;
    CreateInfo.FloatParameters={InColor.R,InColor.G,InColor.B};
    CreateInfo.ParameterDescriptions.Add(CString(U"颜色"),SMaterialParameterDescription(EMaterialParameterType::Vector3,0));

    TSharedObjectPtr<RMaterial> Material=RMaterial::Create(
        CreateInfo
    );

    return Material;
}

TSharedObjectPtr<RMaterial> RMaterialSubsystem::CopyMaterial(TSharedObjectPtr<RMaterial> InTemplate)
{
    auto Ret= NewObject<RMaterial>();
    Ret->CopyFrom(InTemplate);
    return Ret;
}
